/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2020/2/9 10:24
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.job.setup;

import com.spring.arch.job.dto.JobParam;
import com.spring.arch.common.setup.IApplicationSetup;
import com.spring.arch.common.setup.SetupOrder;
import com.spring.arch.job.dto.JobParam;
import com.spring.arch.job.model.JobGroup;
import com.spring.arch.job.model.JobInfo;
import com.spring.arch.job.model.JobLock;
import com.spring.arch.job.model.JobUser;
import com.spring.arch.job.repository.JobGroupRepository;
import com.spring.arch.job.repository.JobInfoRepository;
import com.spring.arch.job.repository.JobLockRepository;
import com.spring.arch.job.repository.JobUserRepository;
import com.spring.arch.job.web.converter.JobInfoConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * JOB初始化程序
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2020/2/9 10:24
 */
@Slf4j
@Component
@SetupOrder(100)
public class JobSetup implements IApplicationSetup {

    private static final String LOCK_NAME = "schedule_lock";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "e10adc3949ba59abbe56e057f20f883e";
    private static final String GROUP_NAME = "intelligent-estate-job";
    private static final String SUSPEND_TASK_DEL_HANDLER = "suspendTaskDelHandler";
    private static final String QUALITY_CHECK_TASK_GENERATE_HANDLER = "qualityCheckTaskGenerateHandler";
    private static final String INSPECTION_TASK_HANDLER = "inspectionTaskForDayOrWeekHandler";
    private static final String DEVICE_INSPECTION_TASK_HANDLER = "deviceInspectionTaskForDayOrWeekHandler";
    private static final String DEVICE_UPKEEP_TASK_HANDLER = "deviceUpkeepTaskForDayOrWeekHandler";
    private static final String INCOME_CONTRACT_JOB_HANDLER = "incomeContractJobHandler";
    private static final String SECURITY_TASK_GENERATE_HANDLER = "securityTaskGenerateHandler";

    @Autowired
    private JobUserRepository jobUserRepository;
    @Autowired
    private JobLockRepository jobLockRepository;
    @Autowired
    private JobGroupRepository jobGroupRepository;
    @Autowired
    private JobInfoRepository jobInfoRepository;
    @Autowired
    private JobInfoConverter jobInfoConverter;

    @Override
    public boolean initDBData(String... args) {
        log.info("JOB初始化程序|enter");
        initAdminUser();
        initJobLock();
        initJobGroup();
        initJobInfo();
        log.info("JOB初始化程序|end");
        return true;
    }

    private void initAdminUser() {
        Optional<JobUser> jobUser = jobUserRepository.findByUsername(USERNAME);
        if (!jobUser.isPresent()) {
            JobUser user = new JobUser(USERNAME, PASSWORD, 1, null);
            jobUserRepository.save(user);
        }
    }

    private void initJobLock() {
        Optional<JobLock> jobLock = jobLockRepository.findById(LOCK_NAME);
        if (!jobLock.isPresent()) {
            JobLock lock = new JobLock(LOCK_NAME);
            jobLockRepository.save(lock);
        }
    }

    private void initJobGroup() {
        Optional<JobGroup> jobGroup = jobGroupRepository.findByAppName(GROUP_NAME);
        if (!jobGroup.isPresent()) {
            JobGroup group = new JobGroup(GROUP_NAME, "智慧物业JOB服务执行器", 1, 0, null);
            jobGroupRepository.save(group);
        }
    }

    private void initJobInfo() {
        Optional<JobGroup> jobGroup = jobGroupRepository.findByAppName(GROUP_NAME);

        // 任务暂停删除作业
        Optional<JobInfo> suspendTaskDelJob = jobInfoRepository.findByExecutorHandler(SUSPEND_TASK_DEL_HANDLER);
        if (jobGroup.isPresent() && !suspendTaskDelJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setJobCron("0 0 1 * * ?");
            param.setJobDesc("任务暂停删除作业");
            param.setExecutorHandler(SUSPEND_TASK_DEL_HANDLER);
            param.setExecutorRouteStrategy("FIRST");
            param.setExecutorParam("slaJobHandler,remindJobHandler");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 品控任务生成器
        Optional<JobInfo> qualityCheckTaskGenerateJob = jobInfoRepository.findByExecutorHandler(QUALITY_CHECK_TASK_GENERATE_HANDLER);
        if (jobGroup.isPresent() && !qualityCheckTaskGenerateJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(QUALITY_CHECK_TASK_GENERATE_HANDLER);
            param.setJobCron("0 0 2 26 * ?");
            param.setJobDesc("品控任务生成器");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 巡检任务生成器
        Optional<JobInfo> inspectionTaskJob = jobInfoRepository.findByExecutorHandler(INSPECTION_TASK_HANDLER);
        if (jobGroup.isPresent() && !inspectionTaskJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(INSPECTION_TASK_HANDLER);
            param.setJobCron("0 0 2 26 * ?");
            param.setJobDesc("巡检任务生成器");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 设备巡检
        Optional<JobInfo> deviceInspectionTaskJob = jobInfoRepository.findByExecutorHandler(DEVICE_INSPECTION_TASK_HANDLER);
        if (jobGroup.isPresent() && !deviceInspectionTaskJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(DEVICE_INSPECTION_TASK_HANDLER);
            param.setJobCron("0 0 2 26 * ?");
            param.setJobDesc("设备巡检任务");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 设备维保
        Optional<JobInfo> deviceUpkeepTaskJob = jobInfoRepository.findByExecutorHandler(DEVICE_UPKEEP_TASK_HANDLER);
        if (jobGroup.isPresent() && !deviceUpkeepTaskJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(DEVICE_UPKEEP_TASK_HANDLER);
            param.setJobCron("0 0 2 26 * ?");
            param.setJobDesc("设备维保任务");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 应收合同定时任务
        Optional<JobInfo> incomeContractJob = jobInfoRepository.findByExecutorHandler(INCOME_CONTRACT_JOB_HANDLER);
        if (jobGroup.isPresent() && !incomeContractJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(INCOME_CONTRACT_JOB_HANDLER);
            param.setJobCron("0 0 2 * * ?");
            param.setJobDesc("应收合同定时任务");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }

        // 安全检查任务
        Optional<JobInfo> securityTaskGenerateJob = jobInfoRepository.findByExecutorHandler(SECURITY_TASK_GENERATE_HANDLER);
        if (jobGroup.isPresent() && !securityTaskGenerateJob.isPresent()) {
            // 构建job参数
            JobParam param = new JobParam();
            param.setJobGroup(jobGroup.get().getId());
            param.setExecutorHandler(SECURITY_TASK_GENERATE_HANDLER);
            param.setJobCron("0 0 3 * * ?");
            param.setJobDesc("安全检查任务");
            param.setExecutorRouteStrategy("FIRST");
            JobInfo info = jobInfoConverter.jobParamToModel(param);
            jobInfoRepository.save(info);
        }
    }
}
